<?php
/*
	代理中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['op']=='main'){
	set_html('title','代理中心');
	$member=$this->get('member',array('mid'=>$_SESSION['uid']));
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);

	$return['ordernum']=$this->pdo_count('commission',array('mid'=>$_SESSION['uid']));
	
	$return['member']=$member;
	$image=ROOT_AT.'images/jy_friends_shop/'.$_SESSION['uniacid'].'/uid'.$_SESSION['uid'].'.jpg';
	if(!file_exists($image)){
		$url='/jy_friends_shop/myshop/index?mid='.$_SESSION['uid'].'&fromuser='.$_SESSION['uid'];
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
		$path=ROOT_AT.'images/jy_friends_shop/'.$_SESSION['uniacid'];
		if(!file_exists($path)){
				mkdir($path,0700, true);
		}
		if($res){
			file_put_contents($path.'/uid'.$_SESSION['uid'].'.jpg',$res);	
		}
		
	}
	$return['myshop']=$this->pdo_get('shop',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['myshop']['appcode']='images/jy_friends_shop/'.$_SESSION['uniacid'].'/uid'.$_SESSION['uid'].'.jpg';
	if(!$return['myshop']['img']){
		$return['myshop']['img']=$member['avatar'];
	}
	$return['myshop']['img']=tomedia($return['myshop']['img']);
	if(!$return['myshop']['title']){
		$return['myshop']['title']=$member['nickname'].'的小店';
	}
	$data=array();
	//已结算佣金
	$data['balance_total']=$this->sum('balance',array('mid'=>$_SESSION['uid']),'money');
	$data['balance_num']=$this->count('balance',array('mid'=>$_SESSION['uid']));
	//待结算佣金
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1);
	$data['commission_1']=$this->sum('commission',$params,'money');
	$data['commission_total']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$return['data']=$data;
	exi($return);
}

